package com.bignerdranch.android.geoquiz;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity {
	private Button mTrueButton;
	private Button mFalseButton;
	private Button mNextButton;
	private Button mCheatButton;
	private TextView mQuestionTextView;
	private TextView mBuildVersion;
	private TrueFalse[] mQuestionBank;
	private int mCurrentIndex = 0;
	private boolean mIsCheater;

	// Constants
	private static final String TAG = "QuizActivity";
	private static final String KEY_INDEX = "index";

	@TargetApi(11)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(savedInstanceState != null){
			mCurrentIndex = savedInstanceState.getInt(KEY_INDEX);
		}
		Log.d(TAG, "onCreate called");
		setContentView(R.layout.activity_quiz);
		
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
			// Action Bar
			ActionBar actionbar = getActionBar();
			actionbar.setSubtitle("Oh my God");
		}
		
		
		// Set views
		mTrueButton = (Button) findViewById(R.id.true_button);
		mFalseButton = (Button) findViewById(R.id.false_button);
		mNextButton = (Button) findViewById(R.id.next_button);
		mCheatButton = (Button) findViewById(R.id.cheat_button);
		mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
		mBuildVersion = (TextView) findViewById(R.id.build_version);
		
		// Set build version
		String version = "API Level " + Build.VERSION.SDK_INT;
		mBuildVersion.setText(version);

		// Set questions
		mQuestionBank = new TrueFalse[] {
				new TrueFalse(R.string.question_oceans, true),
				new TrueFalse(R.string.question_mideast, false),
				new TrueFalse(R.string.question_africa, false),
				new TrueFalse(R.string.question_americas, true),
				new TrueFalse(R.string.question_asia, true) };
		updateQuestion();

		// Set Listeners
		mNextButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				nextQuestion();
			}
		});
		mQuestionTextView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				nextQuestion();
			}
		});
		mTrueButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showAnswer(true);
			}
		});
		mFalseButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showAnswer(false);
			}
		});
		mCheatButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(QuizActivity.this, CheatActivity.class);
				intent.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, mQuestionBank[mCurrentIndex].isAnswer());
				startActivityForResult(intent, 0);
			}
		});
	}

	private void nextQuestion() {
		if (mCurrentIndex + 1 < mQuestionBank.length) {
			mIsCheater = false;
			mCurrentIndex++;
			updateQuestion();
		}
	}

	private void showAnswer(boolean answerByUser) {
		int messageId = 0;
		
		if(mIsCheater){
			messageId = R.string.judgment_toast;
		}else{
			if (mCurrentIndex < mQuestionBank.length) {
				if (mQuestionBank[mCurrentIndex].isAnswer() == answerByUser) {
					messageId = R.string.message_correct;
				} else {
					messageId = R.string.message_incorrect;
				}
			}
		}
		
		Toast.makeText(QuizActivity.this, messageId, Toast.LENGTH_SHORT).show();
	}

	private void updateQuestion() {
		int question = mQuestionBank[mCurrentIndex].getQuestion();
		mQuestionTextView.setText(question);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.quiz, menu);
		return true;
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.d(TAG, "onStart() called");
	}

	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause() called");
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume() called");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy() called");
	}

	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "onStop() called");
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		Log.d(TAG, "onSaveInstanceState");
		outState.putInt(KEY_INDEX, mCurrentIndex);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(data != null){
			mIsCheater = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, false);
		}
		
	}

}
